package com.example.saifly.ReceiverAndService;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.widget.Toast;

import com.example.common.Application.MyApplication;

import static android.app.PendingIntent.FLAG_UPDATE_CURRENT;

public class BootBroadcastReceiver extends BroadcastReceiver {
    private SharedPreferences sharedPreferences = MyApplication.getContext().getSharedPreferences("config", Context.MODE_PRIVATE);
    @Override
    public void onReceive(Context context, Intent intent) {
        SetAlarmReceiver(context);                          //开启每日计划提醒和健康评估的广播
    }

    //立即设置广播，因为没有startBroadcast，所以用0秒延时来代替
    private void SetAlarmReceiver(Context context) {
        Toast.makeText(context, "开始注册广播", Toast.LENGTH_SHORT).show();
        AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);//获取AlarmManager实例
        long systemTime = System.currentTimeMillis();

        Intent intent = new Intent(context, AlarmReceiver.class);
        PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, FLAG_UPDATE_CURRENT);
        manager.set(AlarmManager.RTC_WAKEUP, systemTime, pi);//开启提醒
    }
}
